<?php

namespace backend\controllers;

use backend\server\HouseTypeImageServer;
use common\logic\HouseTypeImageLogic;
use common\logic\HouseTypeImglistLogic;
use Yii;
use common\models\HouseTypeImageModel;
use yii\data\ActiveDataProvider;
use yii\web\NotFoundHttpException;
use common\models\HouseTypeImglistModel;
use yii\helpers\Url;
/**
 * HouseTypeImageController implements the CRUD actions for HouseTypeImageModel model.
 */
class HouseTypeImageController extends BaseController
{

    /**
     * Lists all HouseTypeImageModel models.
     *
     * @return mixed
     */
    public function actionIndex($house_id)
    {
        $dataProvider = new ActiveDataProvider([
            'query' => HouseTypeImageLogic::instance()->getQuery(['house_id' => $house_id], [], [], ['id' => SORT_ASC]),
        ]);
        $dataProvider->sort = false;

        return $this->render('index', [
            'dataProvider' => $dataProvider,
            'house_id' => $house_id
        ]);
    }

    /**
     * Displays a single HouseTypeImageModel model.
     *
     * @param integer $id
     *
     * @return mixed
     */
    public function actionView($id)
    {
        return $this->render('view', [
            'model' => $this->findModel($id)
        ]);
    }

    /**
     * Creates a new HouseTypeImageModel model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     *
     * @return mixed
     */
    public function actionCreate($house_id)
    {
        $model = new HouseTypeImageModel();
        $model->house_id = $house_id;
        $p1 = $p2 = [];
        if ($model->load(Yii::$app->request->post()) && HouseTypeImageServer::instance()->save($model)) {

            // 循环多张商品banner图进行上传和上传后的处理
            for ($i = 0; $i < count($_FILES['HouseTypeImglistModel']['name']['img_path']); $i++) {

                // 调用图片接口上传后返回的图片地址，注意是可访问到的图片地址哦
                $imageUrl = HouseTypeImglistLogic::instance()->saveFile($model->id,$i);
                if(!$imageUrl)
                    continue;
                
                if(!$model->img_path){
                    $model->img_path = $imageUrl;
                    $model->save();
                }

                $url = '/house-type-image/imgdelete';
                $tmodel = new HouseTypeImglistModel();
                $tmodel->typeid = $model->id;
                $tmodel->img_path = $imageUrl;
                $key = 0;
                if ($tmodel->save(false)) {
                    $key = $tmodel->tid;
                }

                $p1[$i] = $imageUrl;
                $p2[$i] = ['url' => $url, 'key' => $key];
            }

            return $this->redirect(['index', 'house_id' => $model->house_id]);
        } else {

            $imgmodel = new HouseTypeImglistModel();

            $images = HouseTypeImglistLogic::instance()->getAll($model->id);
            if ($images) {
                foreach ($images as $k => $v) {
                    $p1[$k] = $v->img_path;
                    $p2[$k] = [
                        // 要删除商品图的地址
                        'url' => Url::toRoute('/house-type-image/imgdelete'),
                        // 商品图对应的商品图id
                        'key' => $v->tid,
                    ];
                }
            }

            return $this->render('create', [
                'model' => $model,
                'p1'=>$p1,
                'p2'=>$p2,
                'imgmodel'=>$imgmodel
            ]);
        }
    }

    /**
     * Updates an existing HouseTypeImageModel model.
     * If update is successful, the browser will be redirected to the 'view' page.
     *
     * @param integer $id
     *
     * @return mixed
     */
    public function actionUpdate($id)
    {
        $model = $this->findModel($id);
        $p1 = $p2 = [];
        $post = Yii::$app->request->post();
        

        if ($model->load($post) && HouseTypeImageServer::instance()->save($model)) {

            // 循环多张商品banner图进行上传和上传后的处理
            for ($i = 0; $i < count($_FILES['HouseTypeImglistModel']['name']['img_path']); $i++) {

                // 调用图片接口上传后返回的图片地址，注意是可访问到的图片地址哦
                $imageUrl = HouseTypeImglistLogic::instance()->saveFile($id,$i);
                if(!$imageUrl)
                    continue;

                if(!$model->img_path){
                    $model->img_path = $imageUrl;
                    $model->save();
                }

                $url = '/house-type-image/imgdelete';
                $tmodel = new HouseTypeImglistModel();
                $tmodel->typeid = $id;
                $tmodel->img_path = $imageUrl;
                $key = 0;
                if ($tmodel->save(false)) {
                    $key = $tmodel->tid;
                }

                $p1[$i] = $imageUrl;
                $p2[$i] = ['url' => $url, 'key' => $key];
            }

            return $this->redirect(['index', 'house_id' => $model->house_id]);
        } else {

            $imgmodel = new HouseTypeImglistModel();

            $images = HouseTypeImglistLogic::instance()->getAll($id);
            if ($images) {
                foreach ($images as $k => $v) {
                    $p1[$k] = $v->img_path;
                    $p2[$k] = [
                        // 要删除商品图的地址
                        'url' => Url::toRoute('/house-type-image/imgdelete'),
                        // 商品图对应的商品图id
                        'key' => $v->tid,
                    ];
                }
            }

            return $this->render('update', [
                'model' => $model,
                'p1'=>$p1,
                'p2'=>$p2,
                'imgmodel'=>$imgmodel
            ]);
        }
    }

    /**
     * Deletes an existing HouseTypeImageModel model.
     * If deletion is successful, the browser will be redirected to the 'index' page.
     *
     * @param integer $id
     *
     * @return mixed
     */
    public function actionDelete($id)
    {
        $model = $this->findModel($id);
        $model->delete();
        HouseTypeImglistModel::deleteAll(['typeid'=>$id]);
        return $this->redirect(['index', 'house_id' => $model->house_id]);
    }

    public function actionImgdelete(){
       if ($id = Yii::$app->request->post('key')) {
            $model = HouseTypeImglistModel::findOne($id);
            if($model!=null){
                $model->delete();
            }
       }
       return $this->asJson(['success' => true]);
    }

    /**
     * Finds the HouseTypeImageModel model based on its primary key value.
     * If the model is not found, a 404 HTTP exception will be thrown.
     *
     * @param integer $id
     *
     * @return HouseTypeImageModel the loaded model
     * @throws NotFoundHttpException if the model cannot be found
     */
    protected function  findModel($id)
    {
        if (($model = HouseTypeImageModel::findOne($id)) !== null) {
            return $model;
        } else {
            throw new NotFoundHttpException('The requested page does not exist.');
        }
    }

    public function getImgList($id){
       return HouseTypeImglistLogic::instance()->getAll($id);
    }
}
